
/**
* Created by yangshengjiepro on 15/4/20.
*/

var LOADINGBARPRONUM = 1;

var LOADINGBAR_TAG = 99912;

var UPDATA_UI_BG = "UPDATA_UI_BG";

var mainLayer_Class = null;

var mainLayer_Root = null;

var basesize = null;

var MainLayer = BaseLayer.extend({
    ctor:function(){
        this._oktouch=true;//开启可透点击
        this._showbg=true;//开启背景
        this._showbgAcion=false;//主界面不需要弹出效果
        this._super();
        mainLayer_Root=this;
        Mlog.c("ok22222");
        basesize = this.getContentSize();

        this.initMapUI(false);

        //初始化进度条
        this.initloadingbar(this);
        //进行异步加载，绑定更新进度条的方法setloadingbar
        GameFrameCache.setAllCache(this,this.setloadingbar);
    },

    initMapUI:function(toRunFlag){
        var random = parseInt(Math.random()*100);
        var mapname = "";
        mapname = "mh";
        var mapname1 = ResMAP + mapname + "_map1.jpg";
        var mapname2 = ResMAP + mapname + "_map1.jpg";
        var mapbg1 =cc.Sprite.create(mapname1);
        this.mapbg1=mapbg1;
        mapbg1.attr({
            x:0,
            y:this.getContentSize().height/2,
            scale:1,
            ratation:0
        });
        Mlog.c("W:",this.getContentSize().width);
        Mlog.c("H:",this.getContentSize().height);
        mapbg1.setAnchorPoint(cc.p(0,0.5));
        this.addChild(mapbg1);
        var basesize = 640;
        var mapbg2 =cc.Sprite.create(mapname2);
        this.mapbg2=mapbg2;
        Mlog.c("SSS22:",basesize.width);
        mapbg2.attr({
            x:basesize,
            y:this.getContentSize().height/2,
            scale:1,
            ratation:0
        });
        Mlog.c("W:",this.getContentSize().width);
        Mlog.c("H:",this.getContentSize().height);
        mapbg2.setAnchorPoint(cc.p(0,0.5));
        this.addChild(mapbg2);

        var mapshan =cc.Sprite.create(res.UI_shan);
        this.mapshan=mapshan;
        Mlog.c("SSS22:",basesize.width);
        mapshan.attr({
            x:0,
            y:180,
            scale:1,
            ratation:0
        });
        mapshan.setAnchorPoint(cc.p(0,0.5));
        this.addChild(mapshan);

        var mapdi =cc.Sprite.create(res.UI_Di);
        this.mapdi=mapdi;
        Mlog.c("SSS22:",basesize.width);
        mapdi.attr({
            x:0,
            y:35,
            scale:1,
            ratation:0
        });
        mapdi.setAnchorPoint(cc.p(0,0.5));
        this.addChild(mapdi);

        var mapLUpai =cc.Sprite.create(res.UI_LUPAI);
        this.mapLUpai=mapLUpai;
        Mlog.c("SSS22:",basesize.width);
        mapLUpai.attr({
            x:0,
            y:120,
            scale:1,
            ratation:0
        });
        mapLUpai.setAnchorPoint(cc.p(0,0.5));
        this.addChild(mapLUpai);

        if(toRunFlag==true)
        {
            this.schedule(this.runMap,0.2);
        }
    },

    initMoveSpUI:function(toRunFlag){

        var sp_yun =cc.Sprite.create(res.UI_yun);
        this.sp_yun=sp_yun;
        sp_yun.attr({
            x:this.getContentSize().width,
            y:this.getContentSize().height/2+250,
            scale:1,
            ratation:0
        });
        Mlog.c("W:",this.getContentSize().width);
        Mlog.c("H:",this.getContentSize().height);
        sp_yun.setAnchorPoint(cc.p(0,0.5));
        this.addChild(sp_yun);


        //var sp_yun2 =cc.Sprite.create(res.UI_yun2);
        //this.sp_yun2=sp_yun2;
        //sp_yun2.attr({
        //    x:this.getContentSize().width,
        //    y:this.getContentSize().height/2-280,
        //    scale:1,
        //    ratation:0
        //});
        //Mlog.c("W:",this.getContentSize().width);
        //Mlog.c("H:",this.getContentSize().height);
        //sp_yun2.setAnchorPoint(cc.p(0,0.5));
        //this.addChild(sp_yun2);

        if(toRunFlag==true)
        {
            this.schedule(this.runSpTag);
        }

    },

    //循环地图移动
    runMap:function(){

        var basesize = this.mapbg1.getContentSize();
        Mlog.c("basesize>>>",basesize.width);
        if(this.mapbg1!=null&&this.mapbg2!=null)
        {
            this.mapbg1.setPositionX(this.mapbg1.getPositionX()-1);
            this.mapbg2.setPositionX(this.mapbg2.getPositionX()-1);
            if(this.mapbg1.getPositionX()<=-basesize.width)
            {
                this.mapbg1.setPositionX(basesize.width);
            }
            if(this.mapbg2.getPositionX()<=-basesize.width)
            {
                this.mapbg2.setPositionX(basesize.width);
            }
        }
    },

    //循环地图移动
    runSpTag:function(){
        var basesize1 = this.sp_yun.getContentSize();
        if(this.sp_yun!=null)
        {
            this.sp_yun.setPositionX(this.sp_yun.getPositionX()-0.5);
            if(this.sp_yun.getPositionX()<=-basesize1.width)
            {
                this.sp_yun.setPositionX(this.getContentSize().width);
            }
        }
    },

    //循环地图移动
    runMoveMapUI:function(){
        var basesize1 = this.sp_yun.getContentSize();
        if(this.sp_yun!=null)
        {
            this.sp_yun.setPositionX(this.sp_yun.getPositionX()-0.5);
            if(this.sp_yun.getPositionX()<=-basesize1.width)
            {
                this.sp_yun.setPositionX(this.getContentSize().width);
            }
        }
    },

    onEnter:function(){
        this._super();
        //UpdateUIManager.getInstance().addPageUpdateListeners(UPDATA_UI_BG,this.updataUI,this);//绑定事件方法
    },
    onExit:function(){
        this._super();
        //UpdateUIManager.getInstance().removeListeners(UPDATA_UI_BG);//删除事件方法
    },

    //进度条的加载
    initloadingbar:function(sp_loading){
        ////加载Loading条
        //var sp_loadingtiao = new cc.Sprite.create(res.UILOADINGBD_png);
        //this.sp_loadingtiao=sp_loadingtiao;
        //sp_loadingtiao.attr({
        //    x: sp_loading.getContentSize().width/2,
        //    y: sp_loading.getContentSize().height/2-180,
        //    scale: 1,
        //    rotation: 0
        //});
        //sp_loading.addChild(sp_loadingtiao,1);
        //var loadingBar = new ccui.LoadingBar.create(res.UILOADINGBAR_png);
        //this.loadingBar=loadingBar;
        //loadingBar.x = sp_loadingtiao.getContentSize().width/2;
        //loadingBar.y = sp_loadingtiao.getContentSize().height/2;
        //sp_loadingtiao.addChild(loadingBar,2,LOADINGBAR_TAG);
        //loadingBar.setPercent(0);
    },

    //资源loadding buffer进度回调
    setloadingbar:function(){

        var pernum = parseInt(LOADINGBARPRONUM/LOADINGBARPROALLNUM *100);
        Mlog.c("pernum >>" +pernum);
        LOADINGBARPRONUM++;
        //if(this.loadingBar!=null)
        //{
        //    this.loadingBar.setPercent(pernum);
        //}
        //进度条加载完毕进行跳转
        if(pernum==100)
        {
            //加载完毕
            Mlog.c("异步加载资源完毕");

            this.initMoveSpUI(true);

            //显示开始游戏
            UiHelper.initStartUI();
        }
    }
});


var MainScene = cc.Scene.extend({
    onEnter:function(){
        this._super();
        var layer = new MainLayer();
        mainLayerRoot = layer;
        this.addChild(layer);
    }
});